package com.yodawnla.fishing.scene;

import java.util.ArrayList;

import org.anddev.andengine.entity.sprite.Sprite;

import android.view.KeyEvent;

import com.yodawnla.fishing.FishSpot;
import com.yodawnla.fishing.FishSpot.SpotUnLockData;
import com.yodawnla.fishing.Player;
import com.yodawnla.fishing.R;
import com.yodawnla.fishing.hud.GameHud;
import com.yodawnla.fishing.system.Fonts;
import com.yodawnla.fishing.system.Tex;
import com.yodawnla.fishing.system.Values;
import com.yodawnla.fishing.system.YS;
import com.yodawnla.lib.YoActivity;
import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoText;

public class StageScene extends YoScene
{

	int mPage = 0;
	final int ITEM_IN_ONE_PAGE = 8;
	int mMaxPage = (Values.Fish_Spot_Quantity % ITEM_IN_ONE_PAGE == 0 ? Values.Fish_Spot_Quantity / ITEM_IN_ONE_PAGE : Values.Fish_Spot_Quantity
			/ ITEM_IN_ONE_PAGE + 1);

	ArrayList<YoButton> mStageBtnList;
	ArrayList<YoText> mStageTextList;
	ArrayList<FishSpot> mFishSpotList;
	ArrayList<YoText> mUnLockTextList;
	YoText mPageText;

	@Override
	public void loadScene()
	{
		Sprite bg = new Sprite(0, 0, 480, 800, getTexture(Tex.StageBg));
		this.attachChild(bg);

		mStageBtnList = new ArrayList<YoButton>();
		mFishSpotList = new ArrayList<FishSpot>();
		mStageTextList = new ArrayList<YoText>();
		mUnLockTextList = new ArrayList<YoText>();

		FishSpot fishSpot;

		for (int i = 0; i < ITEM_IN_ONE_PAGE; i++)
		{

			final int index = i;

			YoButton stageBtn = new YoButton(this, 0, 0, getTexture(Tex.StageBtn))
			{
				@Override
				public void onClickedEvent()
				{
					selectSpot(index);
				}
			};

			this.attachChild(stageBtn);
			mStageBtnList.add(stageBtn);

			YoText stageText = new YoText(0, 0, Fonts.White20, 20);
			stageBtn.attachChild(stageText);
			mStageTextList.add(stageText);

			YoText unLockText = new YoText(0, 20, Fonts.White20, 40);
			stageBtn.attachChild(unLockText);
			mUnLockTextList.add(unLockText);

		}

		for (int i = 0; i < ITEM_IN_ONE_PAGE / 2; i++)
			for (int j = 0; j < 2; j++)
			{
				mStageBtnList.get(i * 2 + j).setPosition(20 + 220 * j, 80 + 132 * i);
			}

		YoButton prevBtn = new YoButton(this, 23, 664, getTexture(Tex.Arrow).deepCopy())
		{
			@Override
			public void onClickedEvent()
			{
				if (mPage == 0)
					mPage = mMaxPage - 1;
				else
					mPage--;

				update();
			}
		};
		prevBtn.setFlippedHorizontal(true);
		this.attachChild(prevBtn);

		mPageText = new YoText(220, 684, Fonts.White20, 2);
		this.attachChild(mPageText);
		mPageText.setText("" + mPage);

		YoButton nextBtn = new YoButton(this, 377, 664, getTexture(Tex.Arrow).deepCopy())
		{
			@Override
			public void onClickedEvent()
			{
				if (mPage == mMaxPage - 1)
					mPage = 0;
				else
					mPage++;

				update();
			}
		};
		this.attachChild(nextBtn);

		YoButton closeBtn = new YoButton(this, 410, 10, getTexture(Tex.Close))
		{
			@Override
			public void onClickedEvent()
			{
				toScene(YS.TitleScene);
			}
		};
		this.attachChild(closeBtn);

	}

	@Override
	public void onEnterScene()
	{
		mFishSpotList = Player.getInstance().getFishSpotList();
		update();
	}

	public void selectSpot(int spotIndex)
	{
		int index = spotIndex + mPage * ITEM_IN_ONE_PAGE;
		if (mFishSpotList.get(index).isUnLock() == true)
		{
			Player.getInstance().setCurrentFishSpot(mFishSpotList.get(index));
			toScene(YS.GameScene);
		}

	}

	public void update()
	{
		mPageText.setText("" + (mPage + 1));
		int size = mPage * ITEM_IN_ONE_PAGE;
		int CurrentPageNumber;
		if (mPage < mMaxPage - 1)
			CurrentPageNumber = ITEM_IN_ONE_PAGE;
		else
			CurrentPageNumber = Values.Fish_Spot_Quantity - size;

		for (int i = 0; i < mUnLockTextList.size(); i++)
			mUnLockTextList.get(i).setVisible(false);

		for (int i = 0; i < mStageBtnList.size(); i++)
		{
			if (i < CurrentPageNumber)
			{
				mStageBtnList.get(i).setVisible(true);
				int type = mFishSpotList.get(i + size).getFishSpotType();
				ArrayList<SpotUnLockData> unLockDataList = mFishSpotList.get(i + size).getSpotUnlockData();

				if (mFishSpotList.get(i + size).isUnLock() == true)
				{
					String text = YoActivity.getBaseActivity().getRString(R.string.Spot1 + type);
					mStageTextList.get(i).setText(text);

				}
				else
				{
					mStageTextList.get(i).setText("?????");
					mUnLockTextList.get(i).setVisible(true);

					String unlockDescriptionString = getRString(R.string.UnLcokRQ) + "\n" + getRString(R.string.FishGot);
					for (int j = 0; j < unLockDataList.size(); j++)
					{
						int unlockType = unLockDataList.get(j).getFishType();
						int hitTimes = unLockDataList.get(j).getHitTimes();
						unlockDescriptionString += getRString(R.string.fish1 + unlockType) + hitTimes + getRString(R.string.times) + "," + "\n";
					}

					mUnLockTextList.get(i).setText(unlockDescriptionString);

				}
			}
			else
			{
				mStageBtnList.get(i).setVisible(false);
			}
		}
	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				GameHud.getInstance().hide();
				toScene(YS.TitleScene);
				return true;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{

				return true;
			}
			break;
		}
		return false;
	}
}
